package com.eportal.action.background;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;

import com.eportal.action.BaseAction;
import com.eportal.domain.LifeTel;
import com.eportal.domain.Sysopt;
import com.eportal.service.LifeTelService;
import com.eportal.util.CityUtil;
import com.eportal.util.ValidateUtil;
import com.framework.jqgrid.JqGridPager;
import com.opensymphony.xwork2.ModelDriven;

/**
 * LifeTelAction
 *
 * @author chencheng
 * @date 2016/05/16
 */
public class LifeTelAction extends BaseAction implements ModelDriven<LifeTel> {

    private Logger log = Logger.getLogger(SysOptAction.class.getName());

    Map<String, Object> result = new HashMap<String, Object>();
    @Autowired
    private LifeTelService lifeTelService;
	/**
     * 便民电话页面
     */
    public String listView() {
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	getRequest().setAttribute("userRole", userRole);
        return SUCCESS;
    }
    
    /**
     * 便民电话列表
     */
    public void page() throws Exception {
    	JqGridPager page = new JqGridPager();
        String agtCode = getRequest().getParameter("agtCode");
    	String communityId = getRequest().getParameter("communityId");
    	String areaCode = getRequest().getParameter("areaCode");
    	String tel = getRequest().getParameter("tel");
    	String cName = getRequest().getParameter("cName");
    	
    	Sysopt  user= (Sysopt)getSession().getAttribute("user");
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	StringBuilder where = new StringBuilder(" t.status=1 ");
    	if (userRole == 1 || userRole == 2 || userRole == 21) {
    		if (!StringUtils.isBlank(agtCode)) {
                where.append(" and t.agtCode = '");
                where.append(agtCode);
                where.append("'");
            }
        	if (!StringUtils.isBlank(communityId)) {
        		where.append(" and t.communityId = '");
        		where.append(communityId);
        		where.append("'");
        	}
        	if (userRole == 21) {
				where.append(" and a.sysoptId=");
				where.append(user.getSid());
			}
		}else	if (userRole == 3 || userRole == 4) {
			where.append(" and t.agtCode ='");
			where.append(user.getAgtCode());
			where.append("'");
			if (!StringUtils.isBlank(communityId)) {
        		where.append(" and t.communityId = '");
        		where.append(communityId);
        		where.append("'");
        	}
		}else if (userRole == 5 || userRole == 6) {
    		where.append(" and t.communityId ='");
    		where.append(user.getCommunityId());
    		where.append("'");
    	}
    	
    	
    	if (StringUtils.isNotBlank(areaCode)) {
    		where.append(" and  substr(c.areaCode,1,");
        	if(CityUtil.isMunicipalities(areaCode)){//直辖市
        		where.append("2) = '");
        		where.append(areaCode.substring(0, 2));
        		where.append("'");
        	}else{									
        		where.append("4) = '");
        		where.append(areaCode.substring(0, 4));
        		where.append("'");
        	}
        }
    	if (!StringUtils.isBlank(tel)) {
    		where.append(" and (t.tel1 like '%");
    		where.append(tel);
    		where.append("%' or t.tel2 like '%");
    		where.append(tel);
    		where.append("%')");
    	}
    	if (!StringUtils.isBlank(cName)) {
    		where.append(" and t.cName like '%");
    		where.append(cName);
    		where.append("%'");
    	}
    	
    	page.setWhere(where.toString());
        setPage(getRequest(), page);
        
        List<Map<String, Object>> lifeTels = lifeTelService.list(page);
        jqGridQueryMap(LifeTel.class, getRequest(), lifeTels, getResponse(), page);
    }
    
    
	/**
     * 便民电话新增
     */
    public String addView() {
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	getRequest().setAttribute("userRole", userRole);
        return SUCCESS;
    }
    
    /**
     * 便民电话保存
     */
    public void add(){
        String msg = ValidateUtil.validate(lifeTel);
		if (msg.length() == 0) {
			LifeTel lifeTel = getModel();
	    	result = lifeTelService.addMethod(lifeTel);
		} else {
			result.put("code", 0);
			result.put("msg", msg);
		}
		responseBody(result);
    }
   
    /**
     * 所有的合作方列表
     */
    public void listAgt() throws Exception {
    	Sysopt  user= (Sysopt)getSession().getAttribute("user");
    	int userRole =  (Integer) getSession().getAttribute("userRole");
    	StringBuilder where = new StringBuilder();
    	if (userRole == 21) {
    		where.append(" and a.sysoptId =");
			where.append(user.getSid());
    	}else if (userRole == 3 || userRole == 4) {
			where.append(" where t.agtCode ='");
			where.append(user.getAgtCode());
			where.append("' ");
		}else if (userRole == 5 || userRole == 6) {
//			return;
		}
    	List<Map<String, Object>> lifeTels = lifeTelService.queryBySql(where.toString());
        responseBody(lifeTels);
    }

    /**
     * 便民电话更新页面
     */
    public String editView() {
		String sid = getRequest().getParameter("sid");
		if (StringUtils.isBlank(sid)) {
			return "error";
		}
		LifeTel lifeTel = lifeTelService.get(Long.parseLong(sid));
		getRequest().setAttribute("lifeTel", lifeTel);
		int userRole =  (Integer) getSession().getAttribute("userRole");
    	getRequest().setAttribute("userRole", userRole);
		return SUCCESS;
	}

    /**
     * 便民电话更新保存
     */
    public void update(){
    	String msg = ValidateUtil.validate(lifeTel);
		if (msg.length() == 0) {
			LifeTel lifeTel = getModel();
	    	result = lifeTelService.updateMethod(lifeTel);
		} else {
			result.put("code", -1);
			result.put("msg", msg);
		}
		responseBody(result);
    }

    /**
     * 便民电话删除
     */
    public void delete(){
    	String ids = getRequest().getParameter("ids");
    	result = lifeTelService.deleteMethod(ids);
        responseBody(result);
    }
    
    
    @Override
    public LifeTel getModel() {
        return lifeTel;
    }

    private LifeTel lifeTel = new LifeTel();
}
